<?php
if(!empty($_SERVER["QUERY_STRING"])){
	echo "error";
	exit;
}
date_default_timezone_set("Asia/Shanghai");
include 'db.php';//数据库连接	
$today = date('Y-m-d',time()-3600*24*1);

echo "$today 投标英雄榜";
echo "<hr>";

$limit = 20;
$start = strtotime($today." 00:00:00");
$end = strtotime($today." 00:00:00")+3600*24;

//echo $start."-------------".$end;
$sql = "SELECT p1.user_id,SUM(p1.account) AS money,p2.username,p2.realname ,MAX(p1.add_time) AS oaddtime 
	FROM ych_borrow_tender AS p1 
	LEFT JOIN ych_user AS p2 ON p1.user_id=p2.user_id 
	LEFT JOIN ych_borrow AS p3 ON p1.borrow_id=p3.id 
	WHERE p1.add_time>=$start AND p1.add_time<=$end AND p3.ftype!=5 and p3.is_day=0 
	GROUP BY p1.user_id ORDER BY money DESC,oaddtime ASC LIMIT $limit";

$data = db_fetch_arrays($sql,$conn);
///////////////////今日统计//////////////////////
$start = strtotime(date('Y-m-d'));
$end = $start+(3600*24);

$sql = "SELECT p1.user_id,SUM(p1.account) AS money,p2.username,p2.realname ,MAX(p1.add_time) AS oaddtime 
	FROM ych_borrow_tender AS p1 
	LEFT JOIN ych_user AS p2 ON p1.user_id=p2.user_id 
	LEFT JOIN ych_borrow AS p3 ON p1.borrow_id=p3.id 
	WHERE p1.add_time>=$start AND p1.add_time<=$end AND p3.ftype!=5 and p3.is_day=0 
	GROUP BY p1.user_id ORDER BY money DESC,oaddtime ASC LIMIT 16";

$data2 = db_fetch_arrays($sql,$conn);

///////////////////月统计//////////////////////
$start = strtotime("2014-03-01 00:00:00");
$end = strtotime("2014-04-01 00:00:00");

$sql = "SELECT p1.user_id,SUM(p1.account) AS money,p2.username,p2.realname ,MAX(p1.add_time) AS oaddtime 
	FROM ych_borrow_tender AS p1 
	LEFT JOIN ych_user AS p2 ON p1.user_id=p2.user_id 
	LEFT JOIN ych_borrow AS p3 ON p1.borrow_id=p3.id 
	WHERE p1.add_time>=$start AND p1.add_time<=$end AND p3.ftype!=5 and p3.is_day=0 
	GROUP BY p1.user_id ORDER BY money DESC,oaddtime ASC LIMIT 16";

$data3 = db_fetch_arrays($sql,$conn);

///////////////////当月统计//////////////////////
$date = date('Y-m-').'01';
$start = strtotime($date);
$end = time();

$sql = "SELECT p1.user_id,SUM(p1.account) AS money,p2.username,p2.realname ,MAX(p1.add_time) AS oaddtime 
	FROM ych_borrow_tender AS p1 
	LEFT JOIN ych_user AS p2 ON p1.user_id=p2.user_id 
	LEFT JOIN ych_borrow AS p3 ON p1.borrow_id=p3.id 
	WHERE p1.add_time>=$start AND p1.add_time<=$end AND p3.ftype!=5 and p3.is_day=0 
	GROUP BY p1.user_id ORDER BY money DESC,oaddtime ASC LIMIT 16";

$data4 = db_fetch_arrays($sql,$conn);

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<style type="text/css">
table {
    background-color: #FFFFFF;
    border: 2px none;
    text-align: center;
}
</style>
</head>
<body>
<table cellspacing="1" style="width:600px;">
<tr>
	<td>排名</td>
	<td>用户名</td>
	<td>投标累计金额</td>
</tr>
<?php 
$i = 1;
foreach ($data as $val){
?>
<tr>
	<td><?php echo $i; ?></td>
	<td><?php echo isset($val['username']) ? strdisplay_start($val['username'],4) : 0; ?></td>
	<td><?php echo isset($val['money']) ? $val['money'] : 0; ?></td>
</tr>
<?php 
$i=$i+1;
}
?>
</table>
<hr />
<?php 
echo date('Y-m-d')." 投标英雄榜";
echo "<hr>";
?>
<table cellspacing="1" style="width:600px;">
<tr>
	<td>排名</td>
	<td>用户名</td>
	<td>投标累计金额</td>
</tr>
<?php 
$i = 1;
foreach ($data2 as $val){
?>
<tr>
	<td><?php echo $i; ?></td>
	<td><?php echo isset($val['username']) ? strdisplay_start($val['username'],4) : 0; ?></td>
	<td><?php echo isset($val['money']) ? $val['money'] : 0; ?></td>
</tr>
<?php 
$i=$i+1;
}
?>
</table>


<hr>
<?php 
echo "03月投标英雄榜";
echo "<hr>";
?>
<table cellspacing="1" style="width:600px;">
<tr>
	<td>排名</td>
	<td>用户名</td>
	<td>投标累计金额</td>
</tr>
<?php 
$i = 1;
foreach ($data3 as $val){
?>
<tr>
	<td><?php echo $i; ?></td>
	<td><?php echo isset($val['username']) ? strdisplay_start($val['username'],4) : 0; ?></td>
	<td><?php echo isset($val['money']) ? $val['money'] : 0; ?></td>
</tr>
<?php 
$i=$i+1;
}
?>
</table>
<hr>
<?php 
echo "当月投标英雄榜";
echo "<hr>";
?>
<table cellspacing="1" style="width:600px;">
<tr>
	<td>排名</td>
	<td>用户名</td>
	<td>投标累计金额</td>
</tr>
<?php 
$i = 1;
foreach ($data4 as $val){
?>
<tr>
	<td><?php echo $i; ?></td>
	<td><?php echo isset($val['username']) ? strdisplay_start($val['username'],4) : 0; ?></td>
	<td><?php echo isset($val['money']) ? $val['money'] : 0; ?></td>
</tr>
<?php 
$i=$i+1;
}
?>
</table>

</body>
</html>
<?php 
function db_fetch_arrays($sql, $conn) 	{
	$result = mysql_query($sql, $conn);
	$i = 0;
	$_res = array();
	while(@$res = mysql_fetch_array($result,MYSQL_ASSOC)) {
		foreach ($res as $key => $value){
			$_res[$i][$key] = $value;//直接转义
		}
		$i++;
	}
	return $_res;
}

function db_fetch_array($sql,$conn) {
		$result = mysql_query($sql, $conn);
		$res = mysql_fetch_array($result,MYSQL_ASSOC);
		$_res = "";
		if (is_array($res)){
			foreach ($res as $key => $value){
				$_res[$key] =$value;
			}
		}
		$query = $_res;
		return $query;
}
function strdisplay_start($str, $length = 1)
{
	$xing = "**";
	if($str == null || $str == ''){
		return '';
	}
	if(!preg_match("/[^\x80-\xff]/",$str)){
		return mb_substr($str, 0, 1, 'utf-8').$xing;	
	}else{
		return substr($str, 0, $length).$xing;	
	}
}
?>